#include "stdafx.h"

#include "AccessoryTbl.h"



CAccessoryTable::CAccessoryTable()
{
	m_sName = _T("phu_kien");
}
bool CAccessoryTable::checkCode(CString sCode, CAccessory* &pAccessory)
{
	CString sQuery;
	char sQueryChar[400];
	MYSQL_ROW row = NULL;
	mysql_query(conn,"SET character_set_results=utf8");
	sQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_ACCESSORY,FIELD_CODE_ACCESSORY,sCode);
	wcstombs(sQueryChar, (TCHAR*)(const TCHAR*)sQuery, 400);
	mysql_query(conn,sQueryChar);
	MYSQL_RES * pResult = mysql_use_result(conn);
	bool bReturn = false;
	if (pResult)
	{
		row = mysql_fetch_row(pResult);
		if(row)
		{
			pAccessory = new CAccessory;

			pAccessory->setCode(CString(row[0]));
			pAccessory->setCodeProduct(CString(row[1]));
			pAccessory->setCodeStore(CString(row[2]));
			pAccessory->setCount(atoi(row[3]));
			pAccessory->setPriceInput(CString(row[4]));
			pAccessory->setPriceSell(CString(row[5]));
			pAccessory->setPriceWholesale(CString(row[6]));		
			
			bReturn = true;
		}
		mysql_free_result(pResult);
	}
	return bReturn;
}
CAccessory* CAccessoryTable::getByProductCode(const CString& sProductCode)
{
	CAccessory* pAccessory = NULL;;
	CString sQuery;
	char sQueryChar[400];
	MYSQL_ROW row = NULL;
	mysql_query(conn,"SET character_set_results=utf8");
	sQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_ACCESSORY,FIELD_CODE_PRODUCT,sProductCode);
	wcstombs(sQueryChar, (TCHAR*)(const TCHAR*)sQuery, 400);
	mysql_query(conn,sQueryChar);
	MYSQL_RES * pResult = mysql_use_result(conn);	
	if (pResult)
	{
		row = mysql_fetch_row(pResult);
		if(row)
		{
			pAccessory = new CAccessory;

			pAccessory->setCode(CString(row[0]));
			pAccessory->setCodeProduct(CString(row[1]));
			pAccessory->setCodeStore(CString(row[2]));
			pAccessory->setCount(atoi(row[3]));
			pAccessory->setPriceInput(CString(row[4]));
			pAccessory->setPriceSell(CString(row[5]));
			pAccessory->setPriceWholesale(CString(row[6]));		
		}
		mysql_free_result(pResult);
	}
	return pAccessory;
}

CAccessory* CAccessoryTable::getByProductCodeAtStore(const CString& sProductCode,const CString& sStoreCode)
{
	CAccessory* pAccessory = NULL;;
	CString sQuery;
	char sQueryChar[400];
	MYSQL_ROW row = NULL;
	mysql_query(conn,"SET character_set_results=utf8");
	sQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s' AND %s = '%s'"),g_sDatabase,TABLE_NAME_ACCESSORY,FIELD_CODE_PRODUCT,sProductCode,FIELD_CODE_STORE,sStoreCode);
	wcstombs(sQueryChar, (TCHAR*)(const TCHAR*)sQuery, 400);
	mysql_query(conn,sQueryChar);
	MYSQL_RES * pResult = mysql_use_result(conn);	
	if (pResult)
	{
		row = mysql_fetch_row(pResult);
		if(row)
		{
			pAccessory = new CAccessory;

			pAccessory->setCode(CString(row[0]));
			pAccessory->setCodeProduct(CString(row[1]));
			pAccessory->setCodeStore(CString(row[2]));
			pAccessory->setCount(atoi(row[3]));
			pAccessory->setPriceInput(CString(row[4]));
			pAccessory->setPriceSell(CString(row[5]));
			pAccessory->setPriceWholesale(CString(row[6]));		
		}
		mysql_free_result(pResult);
	}
	return pAccessory;
}
bool CAccessoryTable::checkProduct(const CAccessory *pSearch,CAccessory* &pFound)
{
	bool bExist = false;
	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE (%s = '%s' AND %s = '%s')"),
						g_sDatabase,TABLE_NAME_ACCESSORY,
						FIELD_CODE_PRODUCT,pSearch->getCodeProduct(),
						FIELD_PRICE_INPUT,pSearch->getPriceInput());
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{
			bExist = true;
			pFound = new CAccessory;

			pFound->setCode(CString(row[0]));
			pFound->setCodeProduct(CString(row[1]));
			pFound->setCodeStore(CString(row[2]));
			pFound->setCount(atoi(row[3]));
			pFound->setPriceInput(CString(row[4]));
			pFound->setPriceSell(CString(row[5]));
			pFound->setPriceWholesale(CString(row[6]));			
		}
		mysql_free_result(pResult);
	}
	
	return bExist;
}

int CAccessoryTable::getCountAccessory(const CString& sCodeProduct,const CString& sCodeStore)
{
	int nNumber =0;
	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s' AND %s = '%s'"),g_sDatabase,TABLE_NAME_ACCESSORY,FIELD_CODE_PRODUCT,sCodeProduct,FIELD_CODE_STORE, sCodeStore);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = NULL;
		while((row = mysql_fetch_row(pResult)) )
		{
			nNumber += atoi(row[3]);
		}
		mysql_free_result(pResult);
	}
	return nNumber;
}
vector<CAccessory*>  CAccessoryTable::getListAccessory(const CString& sCode)
{
	vector<CAccessory*> vtAcc;

	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_ACCESSORY,FIELD_CODE_PRODUCT,sCode);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = NULL;
		while((row = mysql_fetch_row(pResult)) )
		{
			CAccessory* pAcc = new CAccessory;
			pAcc->setCode(CString(row[0]));		
			pAcc->setCodeProduct(CString(row[1]));		
			pAcc->setCodeStore(CString(row[2]));
			pAcc->setCount(atoi(row[3]));
			pAcc->setPriceInput(CString(row[4]));
			pAcc->setPriceSell(CString(row[5]));
			pAcc->setPriceWholesale(CString(row[6]));
			pAcc->setType(-1);
			
			vtAcc.push_back(pAcc);
		}
		mysql_free_result(pResult);
	}
	return vtAcc;
}
bool CAccessoryTable::updateTable(CAccessory* pAccessory)
{
	if (pAccessory)
	{
		CString sQuery;
		char sQueryChar[400];		
		CString sCondition,sUpdate;
		sCondition.Format(_T("WHERE %s = '%s'"),FIELD_CODE_ACCESSORY,pAccessory->getCode());
		sUpdate.Format(_T("%s = %d"),FIELD_COUNT_ACCESSORY,pAccessory->getCount());
		sQuery.Format(_T("UPDATE %s.%s SET %s %s "),g_sDatabase,TABLE_NAME_ACCESSORY,sUpdate,sCondition);
		wcstombs(sQueryChar, (TCHAR*)(const TCHAR*)sQuery, 400);
		mysql_query(conn,sQueryChar);
		return true;
	}
	return false;
}


bool  CAccessoryTable::InsertAccessory(const CAccessory* pAccessory)
{
	if (pAccessory)
	{

		CString sValue;
		sValue.Format(_T("( '%s','%s','%s', %d,'%s', '%s','%s')")
			,pAccessory->getCode(),pAccessory->getCodeProduct(),pAccessory->getCodeStore(),pAccessory->getCount(),
			pAccessory->getPriceInput(),pAccessory->getPriceSell(),pAccessory->getPriceWholesale());
		CSqlTable::m_sName = TABLE_NAME_ACCESSORY;
			return insertRecord(TBL_ACCESSORY,sValue);
	}
	return false;
}


bool  CAccessoryTable::deleteRecord(CString sKey)
{
	CString sQuery;
	sQuery.Format(_T("DELETE  FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_ACCESSORY, FIELD_CODE_ACCESSORY,sKey) ;
	CT2A ascii(sQuery, CP_UTF8);	
	mysql_query(conn,ascii.m_psz);
	MYSQL_RES * pResult = mysql_use_result(conn);
	if (pResult)
		return true;
	return false;
}

int  CAccessoryTable::getMaxCode()
{
	int nMax = 0;
	MYSQL_RES * pResult = NULL;

	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT %s ,MAX(%s)FROM %s.%s "),FIELD_CODE_ACCESSORY,FIELD_CODE_ACCESSORY,g_sDatabase,TABLE_NAME_ACCESSORY);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{
			CString sTemplate;
			sTemplate = row[1];
			sTemplate.Replace(_T("PK"),_T("0"));
			nMax = _tstoi(sTemplate);
		}
		mysql_free_result(pResult);
	}
	return nMax;
}

bool CAccessoryTable::sellAccossory(CString sCode, int nCount)
{
	bool bResult = false;

	CString sQuery;
	MYSQL_ROW row = NULL;
	sQuery.Format(_T("UPDATE %s.%s SET %s = %d WHERE %s ='%s'"),
		g_sDatabase,TABLE_NAME_ACCESSORY,
		FIELD_COUNT_ACCESSORY,nCount,
		FIELD_CODE_ACCESSORY,sCode);
	CT2A ascii(sQuery, CP_UTF8);	
	if(mysql_query(conn,ascii.m_psz)==0)
		bResult = true;

	return bResult;
}

 bool CAccessoryTable::getTypeAccRemain(const CString& sCodeStore,vector<CString>& vtCodeProduct, vector<int>&vtCount)
 {
	 bool bRes = false;
	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT %s, %s FROM %s.%s WHERE %s = '%s'"),FIELD_CODE_PRODUCT,FIELD_COUNT_ACCESSORY,g_sDatabase,TABLE_NAME_ACCESSORY,FIELD_CODE_STORE,sCodeStore);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = NULL;
		 while((row = mysql_fetch_row(pResult)) )
		 {
			
			vtCodeProduct.push_back(CString(row[0]));
			vtCount.push_back(atoi(row[1]));
		 }
		 bRes = true;
		 mysql_free_result(pResult);
	 }
	 return bRes;
 }